//
//  NSDate+YPDate.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/31.
//

#import "NSDate+YPDate.h"

@implementation NSDate (YPDate)

+ (NSString *)formateData:(NSInteger)timestamp {
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    
    NSDate *currentDate = [NSDate date];
    NSTimeInterval time = [currentDate timeIntervalSinceDate:d];
    NSString *str = @"";
    if (time <= 60) {
        str = @"刚刚";
    }else if (time <= 60*60) {
        int mins = time/60;
        str = [NSString stringWithFormat:@"%d分钟前",mins];
    }else if (time <= 60*60*24) {
        [f setDateFormat:@"YYYY.MM.dd"];
        NSString *needStr = [f stringFromDate:d];
        NSString *nowStr = [f stringFromDate:currentDate];
        
        [f setDateFormat:@"HH:mm"];
        if ([needStr isEqualToString:nowStr]) {
            str = [NSString stringWithFormat:@"今天 %@",[f stringFromDate:d]];
        }else {
            str = [NSString stringWithFormat:@"昨天 %@",[f stringFromDate:d]];
        }
    }else {
        [f setDateFormat:@"yyyy.MM.dd"];
        str = [f stringFromDate:d];
    }
    return str;
}

@end
